package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.MonitorKindBean;
import com.hxkcloud.patient.beans.MonitorKindValueBean;
import com.hxkcloud.patient.viewholders.MonitorInputViewholder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by william_sun on 2016-08-04.
 * Description:患者监测录入，血压血糖等
 */
public class MonitorInputAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private MonitorKindBean kind;
    private LayoutInflater mInflater;

    private String[] mDataset;


    public MonitorInputAdapter(Context mContext, MonitorKindBean kind) {
        this.mContext = mContext;
//        this.kind = kind;
        this.kind = new MonitorKindBean();
        this.kind.setDepartmentId(kind.getDepartmentId());
        this.kind.setKindId(kind.getKindId());
        this.kind.setMaxValue(kind.getMaxValue());
        this.kind.setMinValue(kind.getMinValue());
        this.kind.setName(kind.getName());
        this.kind.setUnit(kind.getUnit());
        this.kind.setTestList(kind.getTestList());
        mInflater = LayoutInflater.from(mContext);
        if(kind!=null){

        if (kind.getValueList() != null && kind.getValueList().size() > 0) {
            mDataset = new String[kind.getValueList().size()];
            List<MonitorKindValueBean> beans = kind.getValueList();
            if(beans!=null && beans.size()>0){
                for(int i = 0;i<beans.size();i++){
                    if(mContext.getResources().getString(R.string.high_value).equals(beans.get(0).getName())){
                        this.kind.setValueList(beans);
                    }else{
                        List<MonitorKindValueBean> beans1 = new ArrayList<>();
                        beans1.add(beans.get(1));
                        beans1.add(beans.get(0));
                        this.kind.setValueList(beans1);
                    }
                    break;
                }
            }
        } else {
            this.kind.setValueList(kind.getValueList());
            mDataset = new String[1];
        }

        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.adapter_monitorinput, null);
        MonitorInputViewholder holder = new MonitorInputViewholder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MonitorInputViewholder viewholder = (MonitorInputViewholder) holder;
        MonitorKindValueBean bean = null;
        if (kind.getValueList() != null && kind.getValueList().size() > 0) {
            bean = kind.getValueList().get(position);
            viewholder.et_num.setFilters(new InputFilter[]{lengthfilter, new InputFilter.LengthFilter(10)});
            if(position == 0){
                viewholder.et_num.setHint(mContext.getResources().getString(R.string.monitor_input_data0));
            }else if(position == 1){
                viewholder.et_num.setHint(mContext.getResources().getString(R.string.monitor_input_data1));
            }else{
                viewholder.et_num.setHint(mContext.getResources().getString(R.string.monitor_input_data));
            }
        } else {
            if(mContext.getResources().getString(R.string.montitor_xuetang_text).equals(kind.getName())){
                viewholder.et_num.setFilters(new InputFilter[]{lengthfilter1, new InputFilter.LengthFilter(10)});
            }else{
                viewholder.et_num.setFilters(new InputFilter[]{lengthfilter, new InputFilter.LengthFilter(10)});
            }
            bean = new MonitorKindValueBean();
            bean.setName(mContext.getResources().getString(R.string.data));
            viewholder.et_num.setHint(mContext.getResources().getString(R.string.monitor_input_data));
        }
        viewholder.tv_test.setText(bean.getName());
        viewholder.tv_unit.setText(kind.getUnit());
        viewholder.et_num.setHintTextColor(mContext.getResources().getColor(R.color.grey_text2));
//        viewholder.et_num.setFilters(new InputFilter[]{lengthfilter, new InputFilter.LengthFilter(4)});
        viewholder.et_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mDataset[position] = s.toString();
                if (TextUtils.isEmpty(s.toString())) {
                    listener.showGrey();
                } else {
                    if (getWordsInput()) {
                        listener.showGreen();
                    } else {
                        listener.showGrey();
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        if (position == kind.getValueList().size() - 1) {
            viewholder.view_line.setVisibility(View.GONE);
        } else {
            viewholder.view_line.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public int getItemCount() {
        if (kind.getValueList() != null && kind.getValueList().size() > 0) {
            return kind.getValueList().size();
        } else {
            return 1;
        }
    }

    public String[] getValues() {
        return mDataset;
    }

    public MonitorKindBean getMonitorKindBean(){
        return this.kind;
    }

    public boolean getWordsInput() {
        for (int i = 0; i < mDataset.length; i++) {
            if (TextUtils.isEmpty(mDataset[i])) {
                return false;
            } else {
                continue;
            }
        }
        return true;
    }


    /**
     * 输入框小数的位数
     */
    private static final int DECIMAL_DIGITS = 1;
    /**
     * 设置小数位数控制
     */
    InputFilter lengthfilter = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {
            // 删除等特殊字符，直接返回
            if ("".equals(source.toString())) {
                return null;
            }
            String dValue = dest.toString();
            String[] splitArray = dValue.split("\\.");
            if (splitArray.length > 1) {
                String dotValue = splitArray[1];
                int diff = dotValue.length() + 1 - DECIMAL_DIGITS;
                if (diff > 0) {
                    return source.subSequence(start, end - diff);
                }
            }
            return null;
        }
    };


    /**
     * 输入框小数的位数
     */
    private static final int DECIMAL_DIGITS1 = 2;
    /**
     * 设置小数位数控制
     */
    InputFilter lengthfilter1 = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {
            // 删除等特殊字符，直接返回
            if ("".equals(source.toString())) {
                return null;
            }
            String dValue = dest.toString();
            String[] splitArray = dValue.split("\\.");
            if (splitArray.length > 1) {
                String dotValue = splitArray[1];
                int diff = dotValue.length() + 1 - DECIMAL_DIGITS1;
                if (diff > 0) {
                    return source.subSequence(start, end - diff);
                }
            }
            return null;
        }
    };


    private onWordsChangeListener listener;

    public void setListener(onWordsChangeListener l) {
        listener = l;
    }

    public interface onWordsChangeListener {
        void showGreen();

        void showGrey();
    }
}
